package domain;

import utils.LoadBufferedImage;
import java.awt.*;
import java.awt.image.BufferedImage;
import utils.Constants.antDirection;

import static utils.Constants.*;


public class Ant {
//    速度
    private int velocity=5;
//    方向，枚举类型：LEFT RIGHT STOP
    private int direction= antDirection.RIGHT.ordinal();
//    蚂蚁在木杆上的位置
    private int position;
//    蚂蚁的id
    private int antId;
//    图片路径
    private final BufferedImage antImg;

//    含参构造
    public Ant(int position,int antId,int velocity){
        this.position=100+position;
        this.antId=antId;
        this.velocity=velocity;
        this.antImg= LoadBufferedImage.loadBufferedImage(antImgPath[this.antId]);
    }
//    私有变量的get set方法
    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
//        100是蚂蚁和木板距离窗口左侧的高度
        this.position = position+100;
    }
    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    /**
     * 根据蚂蚁的方向让蚂蚁运动并且判断其是否到达木杆两端
     */
    public void creep(Pole pole){
        if (direction==antDirection.LEFT.ordinal()){
            position-=velocity;
//            走到左尽头
            if(position<=100){
                position= 100;
                direction=antDirection.STOP.ordinal();
            }
        }else if(direction==antDirection.RIGHT.ordinal()){
            position+=velocity;
//            走到右尽头
            if(position>=pole.getPoleLength()+100){
                position= pole.getPoleLength()+100;
                direction=antDirection.STOP.ordinal();
            }
        }
    }

    /**
     *判断蚂蚁是否到达
     */
    public boolean isReach(){
        return direction==antDirection.STOP.ordinal();
    }

    /**
     * 蚂蚁改变行进方向，判断蚂蚁是否相撞的方法由creepingGame类来执行
     */
    public void changeDirection(){
        if(direction==antDirection.RIGHT.ordinal()){
            direction=antDirection.LEFT.ordinal();
        }else if(direction==antDirection.LEFT.ordinal()){
            direction=antDirection.RIGHT.ordinal();
        }
    }

    /**
     * 绘制蚂蚁
     * @param graphics
     */
    public void drawAnt(Graphics graphics){
        graphics.setColor(new Color(124,234,142));
        graphics.drawImage(antImg,position,200,null);
    }
}
